/***************************************************************************
 *   Copyright (C) 2007 by Leonid Krashenko   *
 *   Leonid.Krashenko@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifndef TODOLIST_H
#define TODOLIST_H

#include <qwidget.h>
#include <qlistview.h>
#include <qdom.h>

//#include "tdo"

class ToDoItem;
class QDragEnterEvent;
class QDragMoveEvent;
class QDragLeaveEvent;
class QDropEvent;

/**
	@author Leonid Krashenko <Leonid.Krashenko@gmail.com>
*/
class ToDoList : public QListView
{
	Q_OBJECT
	public:
	    ToDoList(QWidget* parent=0, const char* name=0);

//	    ~ToDoList();
	
		bool loadFromXMLFile(const QString& filename);
		bool saveToXMLFile(const QString& filename);
		
	public slots:
		void addItem();
		void addAdvancedItem();
		void deleteItem();
/*		void openTask();
		void closeTask();*/
		void addChildItem();
		//void removeItem();
		void incCompletion();
		void decCompletion();
		void incPriority();
		void decPriority();
		//void setCompletion(int);
		
		void itemRenamed(QListViewItem*, int col, const QString& text);
		void itemDoubleClicked(QListViewItem *, const QPoint &, int);
		void itemClicked(QListViewItem *, const QPoint &, int);
		void spacePressed(QListViewItem*);
		void editTask(QListViewItem*);
		
		void copyItem();
		void cutItem();
		void pasteItem();
				
	public:	
		void completeBranch(QListViewItem*);
		void completeAll();
		
		// updateToRoots items by their properties
		void updateToRootAll();
//		void updateToRootItem(QListViewItem*);
		
	protected:
		//void paintEvent(QPaintEvent*);
		//void startDrag();
		void contentsDragEnterEvent(QDragEnterEvent*);
		void contentsDragMoveEvent(QDragMoveEvent*);
		void contentsDragLeaveEvent(QDragLeaveEvent*);
		void contentsDropEvent(QDropEvent*);
//		void contentsMouseMoveEvent()
		
		
		void startDrag();
		
	private:
		void init();
		
		ToDoItem* selectedItem();
		
		unsigned _itemCounter;
//		QListView *_lstToDo;
		ToDoItem* _dropItem;
		ToDoItem* _bufferedItem;
		
		enum EditOperations {
			None, Copy, Cut, Paste
		};
		
		EditOperations _lastOperation;
		
//		QDomElement _header;
//		QDomDocument _domTree;
		
		// get xml header information about todolist user
		void getHeaderInfo(const QDomElement& header);
		// build list view from xml
		void buildTree(ToDoItem* parentItem, const QDomElement& parentElement);
		// save heahello, drag!der info
		void headerToXML(const QDomElement& header);
		// save XML body
		void treeToXML(ToDoItem* parentItem, QDomElement& parent);
};

#endif
